package com.dkd.model.cusServiceCall.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dkd.model.base.constants.PromptMessageEnum;
import com.dkd.model.base.pojo.ReturnData;
import com.dkd.model.base.utils.IdWorker;
import com.dkd.model.base.utils.LoggerUtil;
import com.dkd.model.base.utils.PageUtil;
import com.dkd.model.cusServiceCall.entity.CusServiceCall;
import com.dkd.model.cusServiceCall.service.CusServiceCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Bean
 * @since 2018-09-04
 */
@RestController
@RequestMapping("/cusServiceCall")
public class CusServiceCallController {

    @Value("${IdWorker.workerId}")
    private Integer workerId;

    @Value("${IdWorker.datacenterId}")
    private Integer datacenterId;

    @Autowired
    private CusServiceCallService cusServiceCallService;

    /*
    生成主键ID
     */
    private long getPK(){
        IdWorker idWorker = new IdWorker(workerId, datacenterId);
        return idWorker.nextId();
    }

    // 本地自测获取对象
    private CusServiceCall getOne(int i){
        CusServiceCall cusServiceCall = new CusServiceCall();
        cusServiceCall.setId(getPK()+"");
        cusServiceCall.setBusinessId("一一一"+i);
        cusServiceCall.setCreateTime(new Date());
        cusServiceCall.setErrorCode("二二二"+i);
        cusServiceCall.setPhase("三三三改"+i);
        cusServiceCall.setSendUrl("四四四"+i);
        cusServiceCall.setStatus("0");
        return cusServiceCall;
    }


    /**
     * 插入一条数据
     * http://localhost:8080/cusServiceCall/add
     */
    @RequestMapping(value = "/add")
    public ReturnData<CusServiceCall> add(HttpServletRequest request, CusServiceCall cusServiceCall) {
        LoggerUtil.setDebugLog(getClass(), "CusServiceCallController >> add:测试是否打印log日志~~~~~~~~~~~~~~~~");
        //本地自测得到数据
        cusServiceCall =  getOne(4);
        //第一种方法
        //boolean result = cusServiceCallService.saveOrUpdate(cusServiceCall);
        //第二种方法
        boolean result = cusServiceCall.insert();
        ReturnData<CusServiceCall> rd = new ReturnData<CusServiceCall>(PromptMessageEnum.SUCCEED,"添加成功",cusServiceCall);
        return rd;
    }

    /**
     * 更新一条数据
     * http://localhost:8080/cusServiceCall/updateById
     */
    @RequestMapping(value = "/updateById")
    public ReturnData<CusServiceCall> updateById(HttpServletRequest request, String id) {
        //获取原来的数据
        CusServiceCall cusServiceCall = new CusServiceCall();
        cusServiceCall = cusServiceCall.selectById(id);
        //改变状态值
        cusServiceCall.setStatus("1");
        //第一种更新方法
        //boolean result = cusServiceCallService.updateById(cusServiceCall);
        //第二种更新方法
        boolean result = cusServiceCall.updateById();
        ReturnData<CusServiceCall> rd = new ReturnData<CusServiceCall>(PromptMessageEnum.SUCCEED,"更新成功", cusServiceCall);
        return rd;
    }

    /**
     * 删除一条数据
     * http://localhost:8080/cusServiceCall/deleteById
     */
    @RequestMapping(value = "/deleteById")
    public ReturnData<String> deleteById(HttpServletRequest request, String id) {
        //第一种方法
        //boolean result = cusServiceCallService.removeById(id);
        //第二种方法
        CusServiceCall cusServiceCall = new CusServiceCall();
        boolean result = cusServiceCall.deleteById(id);
        ReturnData<String> rd = new ReturnData<String>(PromptMessageEnum.SUCCEED,"删除成功");
        return rd;
    }

    /**
     * 删除一条数据
     * http://localhost:8080/cusServiceCall/getById
     */
    @RequestMapping(value = "/getById")
    public ReturnData<CusServiceCall> getById(HttpServletRequest request, String id) {
        //第一种方式
        //CusServiceCall cusServiceCall = cusServiceCallService.getById(id);
        //第二种方式
        CusServiceCall cusServiceCall = new CusServiceCall();
        cusServiceCall = cusServiceCall.selectById(id);

        ReturnData<CusServiceCall> rd = new ReturnData<CusServiceCall>(PromptMessageEnum.SUCCEED,"成功获取数据对象", cusServiceCall);
        return rd;
    }

    /**
     * 分页查询所有数据
     * http://localhost:8080/cusServiceCall/queryAllData
     */
    @RequestMapping(value = "/queryAllData")
    public ReturnData<IPage<CusServiceCall>> queryAllData(HttpServletRequest request, int current, int size) {
        IPage<CusServiceCall> pagesData = cusServiceCallService.page(new Page<CusServiceCall>(current, size), null);
        ReturnData<IPage<CusServiceCall>> rd = new ReturnData<IPage<CusServiceCall>>(PromptMessageEnum.SUCCEED,"请求成功",pagesData);
        return rd;
    }


    /**
     * 按查询条件查出数据列表(带分页)
     * http://localhost:8080/cusServiceCall/getDataListByWrapperPage
     */
    @RequestMapping(value = "/getDataListByWrapperPage")
    public ReturnData<IPage<CusServiceCall>> getDataListByWrapperPage(HttpServletRequest request, int current, int size) {
        Wrapper wrapper = new QueryWrapper<CusServiceCall>()
                .lambda().like(CusServiceCall::getPhase, "1")
                .or(e -> e.like(CusServiceCall::getSendUrl, "2"));
        IPage<CusServiceCall> dataListPage = cusServiceCallService.page(new Page<CusServiceCall>(current, size), wrapper);
        ReturnData<IPage<CusServiceCall>> rd = new ReturnData<IPage<CusServiceCall>>(PromptMessageEnum.SUCCEED,"请求成功",dataListPage);
        return rd;
    }

    /**用SQL语句查询数据列表(不带分页)
     * http://localhost:8080/cusServiceCall/getDataListBySql
     */
    @RequestMapping(value = "/getDataListBySql")
    public ReturnData<List<CusServiceCall>> getDataListBySql() {
        List<CusServiceCall> userListPage = cusServiceCallService.selectListBySQL();
        ReturnData<List<CusServiceCall>> rd = new ReturnData<List<CusServiceCall>>(PromptMessageEnum.SUCCEED,"请求成功",userListPage);
        return rd;
    }

    /**用SQL语句查询数据列表(不带分页)
     * http://localhost:8080/cusServiceCall/getDataListMapperBySql
     */
    @RequestMapping(value = "/getDataListMapperBySql")
    public ReturnData<List<CusServiceCall>> getDataListMapperBySql(HttpServletRequest request, Long id) {
        List<CusServiceCall> userListPage = cusServiceCallService.selectByPrimaryId(id);
        ReturnData<List<CusServiceCall>> rd = new ReturnData<List<CusServiceCall>>(PromptMessageEnum.SUCCEED,"请求成功",userListPage);
        return rd;
    }

    /**用SQL语句查询数据列表(带分页)
     * http://localhost:8080/cusServiceCall/selectListMapperPageBySql
     */
    @RequestMapping(value = "/selectListMapperPageBySql")
    public ReturnData<PageUtil<CusServiceCall>> selectListMapperPageBySql(HttpServletRequest request, int current, int size, String status) {
        PageUtil<CusServiceCall> userListPage = cusServiceCallService.selectListPageByMap(current,size,status);
        ReturnData<PageUtil<CusServiceCall>> rd = new ReturnData<PageUtil<CusServiceCall>>(PromptMessageEnum.SUCCEED,"请求成功", userListPage);
        return rd;
    }
}

